package ca.cbc.msb.mediacms;

import java.util.List;

import javax.jms.JMSException;
import javax.naming.NamingException;

import ca.cbc.msb.entity.MSBMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ca.cbc.msb.entity.MSBNotification;
import ca.cbc.msb.enums.QueueEnum;
import ca.cbc.msb.mq.MessageUtil;
import ca.cbc.msb.mq.MessageUtilImpl;
import ca.cbc.msb.util.AppConfig;

public class ChangedMediaNotificationHandler implements NotificationHandler {
    private static Logger logger = LoggerFactory.getLogger(ChangedMediaNotificationHandler.class);
    private static Logger progressLogger = LoggerFactory.getLogger("progress");
    private static final String filterParamString = "filter=Release|put";

    private String sequenceFile=AppConfig.getProperty("nh.changedmedia.sequenceid.filename");




    private MessageUtil messageUtil;

    public ChangedMediaNotificationHandler() {
        try {
            messageUtil = new MessageUtilImpl();
        } catch (NamingException e) {
            logger.error("Error in creating message util", e);
            throw new RuntimeException(e);
        } catch (JMSException e) {
            logger.error("Error in creating message util", e);
            throw new RuntimeException(e);
        }

    }

    @Override
    public void handleMessage(List<MSBNotification> nList) throws NamingException, JMSException {
    	logger.debug("Pushing to ChangeNotificationQueue");
        for (MSBNotification n : nList) {
            MSBMessage message = new MSBMessage();
            message.setNotification(n);

            messageUtil.submitMessage(QueueEnum.ChangeNotificationQueue, message);
            messageUtil.commit();
            progressLogger.info("Change notification received for release {}, media {}", n.getReleaseId(), n.getReleaseId());
        }
    }

    @Override
    public String getFilterParam() {
        return filterParamString;
    }



    @Override
    public String getSequenceFileName(){
        return sequenceFile;
    }
    @Override
    public void cleanUp() {
        logger.info("closing the jms session for Delete Release Change media notification handler");
        messageUtil.disconnect();
    }


}
